﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Foundation Framework Class Library
 * Version 2.00
 * 
 * Windows Management Instrumentation Object Query Class
 *		Provides static method(s) for performing queries against the WMI system.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.Threading;
using System.ServiceProcess;
using System.Windows.Forms;
using System.Management;

namespace Adaptive.Foundation.SysTools
{
    /// <summary>
    /// Provides static method(s) for performing queries against the WMI system.
    /// </summary>
    public sealed class WmiQuery
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="WmiQuery"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is hidden to prevent instantiation.
        /// </remarks>
        private WmiQuery()
        {
        }

        /// <summary>
        /// Gets the specified WMI data.
        /// </summary>
        /// <param name="query">The query to be performed.</param>
        /// <returns>
        /// A <see cref="WmiQueryResultCollection"/> contaning the results.
        /// </returns>
        public static WmiQueryResultCollection[] GetWMIData(string query)
        {
            ManagementObjectSearcher searcher = null;       //Searcher instance.
            ManagementObjectCollection objects = null;      //object List.
            ObjectQuery objectQuery = null;                 //Query object to execute.
            ConnectionOptions options = null;               //WMI connection options.
            ManagementScope scope = null;                   //Management scope specifier.
            string path = string.Empty;
            WmiQueryResultCollection[] returnValue = null;  //Return value.
            int count = 0;                                  //Iteration counter.
            int length = 0;                                 //Iteration limit.

            //Attempt to create the object instances.
            try
            {
                path = @"\root\cimv2";
                options = new ConnectionOptions();
                scope = new ManagementScope(path, options);
                objectQuery = new ObjectQuery(query);
                searcher = new ManagementObjectSearcher(scope, objectQuery);
            }
            catch
            {
            }

            if (searcher != null)
            {
                try
                {
                    objects = searcher.Get();
                    length = objects.Count;
                    returnValue = new WmiQueryResultCollection[length];
                    foreach (ManagementObject resultObject in objects)
                    {
                        returnValue[count] = new WmiQueryResultCollection();
                        foreach (PropertyData data in resultObject.Properties)
                        {
                            try
                            {
                                if (data.Value == null)
                                {
                                    //Substitute empty values for null.
                                    if (data.Type.GetType() == typeof(string))
                                    {
                                        returnValue[count].Add(new WmiQueryResult(data.Name, string.Empty));
                                    }
                                    else if (data.Type.GetType() == typeof(bool))
                                    {
                                        returnValue[count].Add(new WmiQueryResult(data.Name, false));
                                    }
                                    else if (data.Type.GetType() == typeof(uint))
                                    {
                                        returnValue[count].Add(new WmiQueryResult(data.Name, 0));
                                    }
                                    else
                                    {
                                        returnValue[count].Add(new WmiQueryResult(data.Name, (int)0));
                                    }
                                }
                                else
                                {
                                    returnValue[count].Add(new WmiQueryResult(data.Name, data.Value));
                                }
                            }
                            catch (Exception)
                            {
                            }
                        }
                        count++;
                    }
                    objects.Dispose();
                }
                catch (Exception)
                {
                    objects = null;
                }
                searcher.Dispose();
            }
            searcher = null;
            objectQuery = null;
            scope = null;
            options = null;
            objects = null;
            path = null;

            return returnValue;
        }
    }
}